package com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.mvp_lib.View.BaseFragment;
import com.bw.liuyushan.zhuangao4_weektest2.DaoSQL.UserMsg;
import com.bw.liuyushan.zhuangao4_weektest2.DaoSQL.UserMsgUtils;
import com.bw.liuyushan.zhuangao4_weektest2.Entity.UserBean;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_weektest2.R;

import org.greenrobot.eventbus.EventBus;

/**
 * ZhuanGao4_WeekTest2
 * file name is : Reg_Fragment
 * created by Ender on 2021/8/23 9:46.
 * author : 刘雨杉
 * Describe(作用):
 */

public class Reg_Fragment extends BaseFragment<BasePresenterTest> implements IContrackTest.C_ViewTest {

    private EditText name1;
    private EditText pass1;
    private EditText pass11;
    private Button register;
    private Button noregister;
    String username;
    String password;
    @Override
    public int bindLayout() {
        return R.layout.reg_fragment;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip = new BasePresenterTest(new BaseModelTest(), this);

        name1 = (EditText) findViewById(R.id.name1);
        pass1 = (EditText) findViewById(R.id.pass1);
        pass11 = (EditText) findViewById(R.id.pass11);
        register = (Button) findViewById(R.id.register);
        noregister = (Button) findViewById(R.id.noregister);
    }

    @Override
    public void initDate() {
        noregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky("直接登录");
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                username = name1.getText().toString();

                password = pass1.getText().toString();
                String repassword = pass11.getText().toString();
                if (username.isEmpty() && password.isEmpty() && repassword.isEmpty()) {
                    ToastUtils.showShort("信息不能为空");
                } else {
                    if (password.isEmpty()) {
                        ToastUtils.showShort("信息不能为空");
                    } else {
                        if (repassword.isEmpty()) {
                            ToastUtils.showShort("信息不能为空");
                        } else {
                            ip.mpReg(username, password, repassword);
                        }
                    }
                }

            }
        });
    }

    @Override
    public void ShowReg(UserBean userBean) {
        if (userBean.getErrorCode() == 0) {
            ToastUtils.showShort("注册成功");
            //注册 成功进行存储 后续登录用户 都是注册过的 查询用户是唯一的
            UserMsg userMsg = new UserMsg();
            userMsg.setUsername(username);
            userMsg.setPassword(password);
            UserMsgUtils.getInstance().getDaoSession().insert(userMsg);
            EventBus.getDefault().postSticky("注册成功");
        } else {
            ToastUtils.showShort("注册失败");
        }
    }
}
